


var login = new Vue({
    el: "#login_form",
    data: {
        user: {
            id: "0",
            account: "",
            password: "",
            userName: ""
        },
        againPassword: null
    },
    methods: {
        login: function () {
            var type = this.$refs.login_button.innerText;
            console.log(type)

            if (type == "注册") {
                var data = new FormData();
                data.append("userName", this.user.account)
                data.append("password", this.user.password);
                data.append("id", 0)
                data.append("fengShu", 0)
                console.log(data)
                axios.post("/register",
                    data, {
                        headers: {
                            'content-type': 'application/json'
                        }
                    }).then(res => {
                    console.log(res)
                    if (res.data == 1) {
                        exit.$options.methods.show_exit();
                        this.show(res)
                        alert("注册成功")
                        this.loginMethod()
                    } else {
                        alert("账号已存在");
                    }
                }).catch(err => {
                    console.error(err)
                })
            } else if (type == "登入") {
                this.loginMethod();
            }

        },

        loginMethod: function () {
            axios.post("/login", {
                account: this.user.account,
                password: this.user.password
            }).then(res => {
                console.log(this.user.account)
                console.log(res)
                console.log(res.data.id);
                if (res.data.id > 0) {
                    exit.$options.methods.show_exit();
                    this.user.id = res.data.id;
                    this.user.userName = res.data.userName
                    this.show(res)
                } else {
                    alert("登入失败，请检查账号密码！")
                }
            })
                .catch(err => {
                    console.error(err);
                })
        },

        show: function (res) {
            var user_box = document.getElementById("user_box")
            user_box.classList.remove("hide")
            var login_table = document.getElementById("login_table")
            login_table.classList.add("hide");
            var userName = document.getElementById("userName")
            userName.innerText = res.data.userName
        }
    },
})
var loginModel = document.getElementById("loginModel");
var login_button = document.getElementById("login_button");
var message = document.getElementById("message");
var againPassword = document.getElementById("againPassword");


var exit = new Vue({
    el: "#show_exit",
    methods: {
        show_exit: function () {
            loginModel.style.display = "none";
            message.style.display = "none";
            againPassword.style.display = "none";
        }
    },
})


new Vue({
    el: "#register_show",
    methods: {
        register_show: function () {
            loginModel.style.display = "block";
            message.style.display = "block";
            againPassword.style.display = "block";
            login_button.innerText = "注册";
        }
    },
})

new Vue({
    el: "#login_show",
    methods: {
        login_show: function () {
            loginModel.style.display = "block"

            login_button.innerText = "登入";
        }
    },
})
